//
//  JMNavigationController.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/21.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMNavigationController.h"
#import "JMTabBarController.h"

@interface JMNavigationController () <UINavigationControllerDelegate>
@property (strong, nonatomic) id Delegates;
@end

@implementation JMNavigationController

+ (void)initialize {
    // 设置导航栏主题
    [self setUpNavigationBar];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 设置代理
    self.delegate = self;
    self.Delegates = self.interactivePopGestureRecognizer.delegate;
}

+ (void)setUpNavigationBar {
    //设置NavigationBar背景颜色
    [[UINavigationBar appearance] setBarTintColor:JMColor(62, 133, 192)];

    [[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// 设置状态栏的颜色为白色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

/**
 *  拦截push进来的控制器
 *
 *  @param viewController push进来的控制器
 *  @param animated       动画效果
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 当跳转时隐藏底部
    self.tabBarController.tabBar.hidden = YES;
  
    if (self.viewControllers.count > 0) { // 此时push进来的控制器不是根控制器
        // 设置导航栏左侧内容
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(0, 0, 70, 44);
        [btn setImage:[UIImage imageNamed:@"Back"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"BackPush"] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    }

    [super pushViewController:viewController animated:YES];
}

/**
 *  返回上一层
 */
- (void)back {
    if ( 2 == self.viewControllers.count) { // 返回到最底层时显示tabbar;
        self.tabBarController.tabBar.hidden = NO;
    }
    [self popViewControllerAnimated:YES];
}

#pragma mark - navigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 登陆和我的需要隐藏导航条
    if ([NSStringFromClass([viewController class]) isEqualToString:@"JMProfileViewController"] || [NSStringFromClass([viewController class]) isEqualToString:@"JMLoginViewController"]) {
        self.navigationBar.hidden = YES;
    } else {
        self.navigationBar.hidden = NO;
    }
    
    if (viewController == self.viewControllers[0]) { // 根控制器,还原滑动返回代理, 显示tabbar
        self.tabBarController.tabBar.hidden = NO;
        self.interactivePopGestureRecognizer.delegate = self.Delegates;
        
    } else {
        self.interactivePopGestureRecognizer.delegate = nil;
    }
}
@end
